﻿using com.fmtx.IDal;
using com.fmtx.Model;
using fmtx.Framework.DALFactory;
using fmtx.Framework.Model;
using fmtx.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace website.Areas.UCenter.Controllers
{
    public class PhotoController : BaseController
    {
        //
        // GET: /UCenter/Photo/
        private IPictureType pictureType = DataAccess<IPictureType>.GetObject();
        private IPicture picture = DataAccess<IPicture>.GetObject();

        #region 相册列表
        /// <summary>
        /// 相册列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int uid)
        {
            var models = pictureType.Query(uid);
            return View(models);
        }
        #endregion

        #region 相册
        /// <summary>
        /// 相册
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Detail(int uid,int id)
        {
            ViewBag.bean = pictureType.Get(id);
            var models = picture.QueryByUser(uid, id);
            return View(models);
        }
        #endregion

        #region 图片编辑
        /// <summary>
        /// 图片编辑
        /// </summary>
        /// <param name="typeid"></param>
        /// <param name="images"></param>
        /// <returns></returns>
        public ActionResult SaveImage(int typeid, string images)
        {
            ViewBag.typeid = typeid;
            var strs = images.Split(',');
            return View(strs);
        }
        #endregion

        #region 保存图片列表
        /// <summary>
        /// 保存图片列表
        /// </summary>
        /// <param name="models"></param>
        /// <param name="typeid"></param>
        /// <returns></returns>
        public ActionResult DoSaveImg(List<Picture> Photo, int typeid)
        {
            var type = pictureType.Get(typeid);
            if (Photo != null)
            {
                foreach (var item in Photo)
                {
                    try
                    {
                        string file = StaticCs.SaveTempPic(item.PicAddress);
                        if (string.IsNullOrEmpty(type.FrontCover))
                        {
                            type.FrontCover = file;
                            pictureType.Update(type);
                        }
                        item.TypeId = typeid;
                        item.PicAddress = file;
                        item.Scale = ImageFactory.GetScale(Server.MapPath(StaticCs.VirtualPath + file));
                        item.Uid = userInfo.Uid;
                        item.InTime = DateTime.Now;
                        try
                        {
                            item.KeyWord = StaticCs.SaveKeyWord(item.Content.RemoveHtml());
                            item.KeyWord = StaticCs.SaveKeyWord(item.KeyWord, Target.PICTRUE);
                            if (!string.IsNullOrEmpty(item.Content))
                            {
                                item.Content = item.Content.FileDirt();
                                List<AreaKey> areas = item.Content.FindArea();
                                if (areas != null)
                                {
                                    var aa = areas.FirstOrDefault();
                                    item.Address = aa.Name;
                                    item.AddressUrl = aa.Url;
                                    item.AreaKeys = areas;
                                }
                            }
                        }
                        catch {
                            fmtx.Framework.Log.Info("内容不足，放弃");
                        }
                        item.Title = item.Title.FileDirt();
                        item.Statu = Approved.IsPass;
                        picture.Add(item);
                        PictureCache.Insert(item);
                    }
                    catch (Exception e)
                    {
                        fmtx.Framework.Log.Error(e);
                        continue;
                    }
                }
            }
            StaticCs.RemoveUserFolder(userInfo.Uid);
            return RedirectToAction("Detail", new { id = typeid });
        }
        #endregion

    }
}
